
    /**  
    * @Title: PortalApiViewAuthServiceImpl.java
    * @Package com.efast.cafe.portal.service.api.impl
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2019年3月25日
    * @version V1.0  
    */
    
package com.efast.cafe.portal.service.api.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.portal.bean.api.PortalApiViewAuthQueryBean;
import com.efast.cafe.portal.bean.api.PortalApiViewAuthSaveBean;
import com.efast.cafe.portal.bean.api.PortalApiViewAuthSaveDetailBean;
import com.efast.cafe.portal.dao.api.PortalApiViewAuthDao;
import com.efast.cafe.portal.entity.api.PortalApiViewAuth;
import com.efast.cafe.portal.service.api.IPortalApiViewAuthService;
import com.efast.cafe.util.StringUtil;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;


@Service
public class PortalApiViewAuthServiceImpl implements IPortalApiViewAuthService {

	@Autowired
	private PortalApiViewAuthDao portalApiViewAuthDao;

	
	  
	    
	@Override
	public List<PortalApiViewAuth> queryAuth(PortalApiViewAuthQueryBean auth) {
		Example example = new Example(PortalApiViewAuth.class);
		Criteria criteria =  example.createCriteria();
		if(auth!=null){
			if(null!=auth.getViewId()){
				criteria.andEqualTo("viewId", auth.getViewId());
			}
			if(auth.getEntityIds()!=null && auth.getEntityIds().length>0){
				criteria.andIn("entityId", Arrays.asList(auth.getEntityIds()));
			}
			if(StringUtil.isNotBlank(auth.getType())){
				criteria.andEqualTo("type", auth.getType());
			}
			if(auth.getAuthId()!=null){
				criteria.andEqualTo("authId", auth.getAuthId());
			}
		}
		return portalApiViewAuthDao.selectByExample(example);
	}

	
	   
	    
	@Override
	public void saveAuths(PortalApiViewAuthSaveBean savebean) {
		Example example = new Example(PortalApiViewAuth.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("viewId",savebean.getViewId());
		portalApiViewAuthDao.deleteByExample(example);
		if(savebean.getAuths()!=null && savebean.getAuths().length>0){
			List<PortalApiViewAuth> list = new ArrayList<>();
			for(PortalApiViewAuthSaveDetailBean dbean : savebean.getAuths()){
				if(StringUtil.isNotBlank(dbean.getInvisibles()) || 
						StringUtil.isNotBlank(dbean.getNonEditables()) || 
						StringUtil.isNotBlank(dbean.getRequired()) ||
						StringUtil.isNotBlank(dbean.getActions())||
						StringUtil.isNotBlank(dbean.getToolbars())){
					PortalApiViewAuth au = new PortalApiViewAuth();
					au.setAuthId(dbean.getAuthId());
					au.setEntityId(dbean.getEntityId());
					au.setInvisibles(dbean.getInvisibles());
					au.setNonEditables(dbean.getNonEditables());
					au.setType(dbean.getType());
					au.setViewId(savebean.getViewId());
					au.setRequired(dbean.getRequired());
					au.setActions(dbean.getActions());
					au.setToolbars(dbean.getToolbars());
					list.add(au);
				}
			}
			if(!list.isEmpty()){
				portalApiViewAuthDao.insertList(list);
			}
		}
	}
}
